package com.turing.data.scripts.contract.filter

import com.turing.data.engine.TextUtils
import com.turing.data.engine.utils.ScriptsUtils
import com.turing.data.vo.out.review.DependParamsVO
import com.turing.data.vo.out.review.TaskElementVO
import org.apache.commons.lang.SerializationUtils
import org.springframework.util.CollectionUtils
import com.turing.common.util.FileUtils

class OtherrMatters_30207_10_filter {

    static List< TaskElementVO.DrawResultVO > doConvert(DependParamsVO dependParams){

        def REGEX_OTHER = '其他事项'

        List<TaskElementVO.DrawResultVO > trueValueSet = new ArrayList<>()
        List<TaskElementVO.DrawResultVO > resultVOs = ScriptsUtils.tryInitResult(dependParams)
        if (resultVOs!=null && resultVOs.size()>0) {
            for (int i = 0; i < resultVOs.size(); i++) {
                TaskElementVO.DrawResultVO drawResultVO = resultVOs[i]
                def childTaskSn = drawResultVO.childTaskSn
                if(null == childTaskSn || childTaskSn == ""){
//                    return  resultVOs
                    continue
                }
                def start = drawResultVO.start
                def end = drawResultVO.end
                def text = drawResultVO.text
                text = text.replaceAll("\n","")
                def textPathMap = dependParams.textPathMap
                def rawPath = textPathMap.get(drawResultVO.childTaskSn)
                def rawText = FileUtils.loadJson(rawPath)
                def expandOffset = 0
                while( expandOffset < 250 && start > expandOffset ){
                    def expandString = rawText.substring(start-expandOffset,end)
                    if( expandString =~ REGEX_OTHER ){
                        def expand = 0
                        while( expand < 1200 && rawText.length() > end + expand ){
                            def expandStr = rawText.substring(end,end+expand)
                            if(expandStr =~ '（以下无正文'){
                                def index = expandString.indexOf(REGEX_OTHER)
                                def startOffset = expandOffset - (REGEX_OTHER.length()+index)
                                def endOffset = expand - 6
                                def JSONTextMap = dependParams.JSONTextMap
                                def fileTextStr = ""
                                fileTextStr = JSONTextMap.get(drawResultVO.childTaskSn)
                                TaskElementVO.DrawResultVO objectNew = (TaskElementVO.DrawResultVO) SerializationUtils.clone(drawResultVO)
                                objectNew = TextUtils.correctPositionInfoByOffset(objectNew, -startOffset, endOffset, fileTextStr, rawText)
                                objectNew.text = rawText.substring( start - startOffset, end + endOffset  )
                                objectNew.businessValue = rawText.substring( start - startOffset, end + endOffset )
                                trueValueSet.add(objectNew)
                                return trueValueSet
                            }
                            expand ++
                        }
                    }
                    expandOffset ++
                }
            }
        }

        if(!CollectionUtils.isEmpty(resultVOs) && resultVOs.size()>2){
            trueValueSet.add(resultVOs[0])
            trueValueSet.add(resultVOs[1])
            return trueValueSet
        }

        return  resultVOs

    }

    public static void main(String[] args) {

        def str = "其他事项\n" +
                "本计划存续期内，管理人应依据法律法规履行相应反洗钱义务，并主动配合\n" +
                "托管账户开立结算银行根据监管部门有关反洗钱要求开展客户身份识别工作,提\n" +
                "供真实、准确、完整的客户资料;根据人民银行有关规定向托管人发送本计划“受\n" +
                "益所有人”信息，遵守各方反洗钱与反恐怖融资相关管理规定。对具备合理理由\n" +
                "怀疑涉嫌洗钱、恐怖融资的客户，任何一方有权按照中国人民银行反洗钱监管规\n" +
                "定采取必要管控措施，或单方面终止本合作。"

        str = str.replaceAll("\\\n","")
        println str.length()

    }

}
